﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessMsgReply: IBusinessMsgReply
    {
        IDALMsgReply _dalReply;
        IUnitOfWork _unitWork;
        public BusinessMsgReply(IDALMsgReply dalReply, IUnitOfWork unitWork)
        {
            _dalReply = dalReply;
            _unitWork = unitWork;
        }
        public void Add(MsgReplyModel model)
        {
            var dataEntity = Mapper.Map<Tbl_MsgReply>(model);
            _dalReply.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(MsgReplyModel model)
        {
            var dataEntity = Mapper.Map<Tbl_MsgReply>(model);
            _dalReply.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<MsgReplyModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_MsgReply>>(models);
            _dalReply.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<MsgReplyModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_MsgReply>>(models);
            _dalReply.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<MsgReplyModel> GetAll(bool isTracking = true)
        {
            var entities = _dalReply.GetAll(isTracking);
            return Mapper.Map<List<MsgReplyModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalReply.Delete(primaryKey);
            _unitWork.Commit();
        }

        public MsgReplyModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalReply.Get(primaryKey);
            return Mapper.Map<MsgReplyModel>(dataEntity);
        }

        public void Delete(MsgReplyModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_MsgReply>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalReply.Delete(where);
            _unitWork.Commit();
        }

        public MsgReplyModel Get(MsgReplyModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_MsgReply>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (model.MsgId != 0)
            {
                where = where.And(x => x.MsgId == model.MsgId);
            }
            var dataEntity = _dalReply.Get(where);
            return Mapper.Map<MsgReplyModel>(dataEntity);
        }

        public List<MsgReplyModel> GetAll(MsgReplyModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_MsgReply>();
            if (model.MsgId != 0)
            {
                where = where.And(x => x.MsgId == model.MsgId);
            }
            var entities = _dalReply.GetAll(where, isTracking);
            return Mapper.Map<List<MsgReplyModel>>(entities);
        }


        public List<MsgReplyModel> GetAll(MsgReplyModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void AddAgree(int id, string account)
        {
            var dataEntity =_dalReply.Get(id);
            dataEntity.AgreeUsers = string.IsNullOrWhiteSpace(dataEntity.AgreeUsers) ? account : dataEntity.AgreeUsers + "&" + account;
            _unitWork.Commit();
        }
    }
}
